$(document).ready(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
	$("#form_flagStatus").combobox({

		onChange: function (obj,showID) {

			if($(this).combobox("getValue")==1){
				$("#islunbo").css('display','table-row'); 
			}else{
				$("#islunbo").css('display','none'); 
			}
		}

		});
});

$(window).resize(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
});

/**
 * 条件质量控制搜索
 */
function searchQualityControl(){
	//s_qcId,  s_projectName, s_sponsor, s_status, s_openFlag, s_ptclsj, s_ptxz
	$("#qualityControl").datagrid("load",{
		"s_qcId":$("#search_qcId").textbox("getValue"),
		"s_projectName":$("#search_projectName").textbox("getValue"),
		"s_sponsor":$("#search_sponsor").textbox("getValue"),
		"s_status":$("#search_status").combobox("getValue"),
		"s_openFlag":$("#search_openFlag").combobox("getValue"),
		"s_ptclsj":$("#search_ptclsj").combobox("getValue"),
		"s_ptxz":$("#search_ptxz").combobox("getValue"),
	});
}



/**格式化使用状态列，显示参与人数*/
function showSponsorFormatter(value, rec, index){
	var sponsor=(rec.sponsorId != null &&rec.sponsorId != "")  ?"<a style='cursor:pointer;color: #2A2AFF;'    onclick=\"openDialog('"+ rec.sponsorId +"')\">"+rec.sponsorCompName+"</a>":"";
	 return sponsor;
}



/**格式化使用状态列，显示参与人数*/
function countAndJoinersFormatter(value, rec, index){
	var countName=rec.countName;
	var joiners =rec.qualityControlJoinersLength==0?"":"(<a  style='cursor:pointer;color: #2A2AFF;'   onclick=\"showJoiners('"+ rec.qcId+"')\">"+rec.qualityControlJoinersLength+"</a>)";
	 return countName+joiners;
}

/**格式化使用状态列，显示是否关闭的状态*/
function openFlagFormatter(value, rec, index){
	var statusName=rec.statusName;
	var openFlag =rec.openFlag ==1?"":"（关闭）";
	 return statusName+openFlag;
}

function priceFormatter(value, rec, index){
	var price=(rec.price != null && rec.price != "") ?rec.price+"元":"0.00元";
	 return price;
}

/**格式化使用状态列，时间只显示年月日模式*/
function dateFormatter(value, rec, index){
	 return value.substr(0, 10);
}

/**格式化使用状态列，时间只显示年月日模式*/
function dateHHmmssFormatter(value, rec, index){
	 return value != null?value.replace("T"," "):"";
}

/**格式化使用状态列，将传入值变成复选框模式*/
function ptclsjStatusFormatter(value, rec, index){
	var img=rec.ptclsj==1?"icon icon-ok":"icon icon-delete3";
	 return "<span class='"+img+"' style='height: 18;'></span>";
}

/**格式化使用状态列，将传入值变成复选框模式*/
function ptxzStatusFormatter(value, rec, index){
	var img=rec.ptxz==1?"icon icon-ok":"icon icon-delete3";
	 return "<span class='"+img+"' style='height: 18;'></span>";
}

/**格式化使用状态列，将传入值变成复选框模式*/
function gbjgxxStatusFormatter(value, rec, index){
	var img=rec.gbjgxx==1?"icon icon-ok":"icon icon-delete3";
	 return "<span class='"+img+"' style='height: 18;'></span>";
}

/**格式化使用状态列，将传入值变成<a>标签*/
function imgsFormatter(value, rec, index){
	var img = "";
	img +=(rec.img1 != null && rec.img1 != "")?"<a class='ml20' style='cursor:pointer;color: #2A2AFF;'    onclick=\"tanchupic('"+ rec.img1.substr(1)+"')\">[图1]</a>":"";
	img +=(rec.img2 != null && rec.img2 != "")?"<a class='ml20' style='cursor:pointer;color: #2A2AFF;'    onclick=\"tanchupic('"+ rec.img2.substr(1)+"')\">[图2]</a>":"";
	img +=(rec.img3 != null && rec.img3 != "")?"<a class='ml20' style='cursor:pointer;color: #2A2AFF;'    onclick=\"tanchupic('"+ rec.img3.substr(1)+"')\">[图3]</a>":"";
	 return img;
}

/**格式化使用状态列，将传入值变成<a>标签*/
function pdfFileFormatter(value, rec, index){
	var pdf = (rec.filePath != null && rec.filePath != "")?"<a style='cursor:pointer;color: #2A2AFF;'  target=_blank  href='"+ rec.filePath.substr(1)+"')>[查看报告]</a>":"";
	 return pdf;
}

function longStringFormatter(value, rec, index){
	var String = (value != null && value != "")?(value.length > 20? value.substr(0, 20)+'……':value):"";
	 return String;
}

function modifyQualityControl(){
	//判断表格是否被选中，如果选中则取消选中
	var item="";
	try{
		item=$('#qualityControl').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要修改的详情项！', 'error');
		return;
	}
	window.open("admin/qualityControl/qualityControl_qualityControlEdit.action?qualityControl.qcId="+item.qcId, '', 'height=645px, width=630px, top='+(window.screen.availHeight-10-645)/2+', left='+(window.screen.availWidth-10-600)/2+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
	
}

function openDialog(sponsorId){
	$('#sponsor_dialog').dialog({
        title: '实验室详情',
    });
	
	$('#compName').text("");
	$('#compNature').text("");
	$('#phone').text("");
	$('#fax').text("");
	$('#email').text("");
	
	$('#sponsor_dialog').dialog('open');
	$.post("admin/qualityControl/qualityControl_sponsorInfo.action?qualityControl.sponsor.accountId=" + sponsorId,
	        function(data) {
			$('#accountId').text(data.sponsor.accountId);
			$('#compName').text(data.sponsor.compName);
			$('#compNature').text(data.sponsor.linkMan);
			$('#phone').text(data.sponsor.phone);
			$('#fax').text(data.sponsor.fax);
			$('#email').text(data.sponsor.email);
    });
}

function closeQualityControl(){
	var item="";
	try{
		item=$('#qualityControl').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要关闭的详情项！', 'error');
		return;
	}
	$('#statusClose_dialog').dialog({
        title: '关闭原因',
    });
	$('#form_qcId_Hidden').val(item.qcId);
	$('#form_closeReason_Hidden').val("");
	//初始化表单值
	$.post("admin/qualityControl/qualityControl_qualityControlEdit.action?qualityControl.qcId="+item.qcId,
	        function(data) {
			$('#form_closeReason_Hidden').val(data.closeReason);
	    });
	
	
    $('#statusClose_form').form({
        url: 'admin/qualityControl/qualityControl_closeQualityControl.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#statusClose_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
    			$("#qualityControl").datagrid("load");
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#statusClose_dialog').dialog('open');
	 
}

function openQualityControl(){
	var item="";
	try{
		item=$('#qualityControl').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要开启的详情项！', 'error');
		return;
	}
	load();
	$.post("admin/qualityControl/qualityControl_openQualityControl.action?qualityControl.qcId="+item.qcId,{},
			function(data) {
		disLoad();
        if (data.result == "Success") {
            $.messager.alert('提示', data.message, 'info');
			$("#qualityControl").datagrid("load");
        } else {
            $.messager.alert('错误', data.message, 'error');
        }
    }
	); 
}

function removeQc(){
	var item="";
	try{
		item=$('#qualityControl').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要删除的详情项！', 'error');
		return;
	}
	if(item.openFlag){
		$.messager.alert('错误', '非关闭状态无法删除！', 'error');
		return;
	}
	load();
	$.post("admin/qualityControl/qualityControl_remove.action?qualityControl.qcId="+item.qcId,{},
			function(data) {
		disLoad();
		var json = JSON.parse(data);
        if (json.result == "Success") {
            $.messager.alert('提示', json.message, 'info');
			$("#qualityControl").datagrid("load");
        } else {
            $.messager.alert('错误', json.message, 'error');
        }
    }
	); 
}


function updatePDF(){
	var item="";
	try{
		item=$('#qualityControl').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要上传的详情项！', 'error');
		return;
	}
	if(item.status < 3 ){
		$.messager.alert('错误', '现阶段不能上传报告！', 'error');
		return;
	}
	$('#updatePDF_dialog').dialog({
        title: '上传报告',
    });
	$('#pdfForm_qcId_Hidden').val(item.qcId);
	 $('#updatePDF_form').form({
        url: 'admin/qualityControl/qualityControl_updateFile.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#updatePDF_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
    			$("#qualityControl").datagrid("load");
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#updatePDF_dialog').dialog('open');
		 
}

function submitPDFForm() {
	$('#updatePDF_form').submit();
}

function dialog_close (){
	$('#sponsor_dialog').dialog('close');
}

function submitDataForm() {
	$('#statusClose_form').submit();
}

function showJoiners(qcId){
	$('#joiners_dialog').dialog({
        title: '参加人详情',
    });
	$('#qualityControlJoiners').datagrid({
	    fit:true,
	    url: 'admin/qualityControl/qualityControl_getJoinersJson.action?joinId='+ qcId,
	    method: 'get',
	    striped: true,
	    fitColumns: true,
	    singleSelect: true,
	    rownumbers: true,
	    pageSize: 10,
	    pageList: [10, 20, 50, 100, 150, 200],
	    pagination:true,
	    showFooter: true,
	    columns: [[
	        { field: 'joinerIdCompName', title: '实验室', width: 300, align: 'left' },
	        { field: 'joinerIdLinkMan', title: '联系人', width: 100, align: 'center' },
			{ field: 'joinerIdPhone', title: '手机号', width: 150, align: 'center' },
			{ field: 'joinerAddress', title: '地址', width: 300, align: 'left' },
			{ field: 'joinerIdFax', title: '传真', width: 150, align: 'center' },
	        { field: 'joinerIdEmail', title: '邮箱', width: 200, align: 'left' },
	    ]]
	});
	 $('#joiners_dialog').dialog('open');
}

function joiners_close (){
	$('#joiners_dialog').dialog('close');
}

//弹出加载层
function load() {  
    $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body");  
    $("<div class=\"datagrid-mask-msg\"></div>").html("正在加载，请稍候。。。").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 });  
}  
  
//取消加载层  
function disLoad() {  
    $(".datagrid-mask").remove();  
    $(".datagrid-mask-msg").remove();  
}

//-------------------------修改页面的Js------------------------------------------------

function myformatter(date){
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function myparser(s){
	if (!s) return new Date();
	var ss = (s.split('-'));
	var y = parseInt(ss[0],10);
	var m = parseInt(ss[1],10);
	var d = parseInt(ss[2],10);
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	} else {
		return new Date();
	}
}

function submitEditDataForm() {
	
	$('#qualityControl_edit_form').form({
        url: 'admin/qualityControl/qualityControl_editQualityControl.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $.messager.alert('提示', json.message, 'info');
                opener.loadDataGrid();
    			window.close();
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });

	
	$('#qualityControl_edit_form').submit();
}
function loadDataGrid(){
	$("#qualityControl").datagrid("load");
}

